package easy.x的平方根;

public class Solution2 {
    public int mySqrt(int x) {
        int l = 0, r = x;
        while (l <= r) {
            int m = (l + r) / 2;
            if ((long) m * m < x) {
                l = m + 1;
            } else if ((long) m * m > x) {
                r = m - 1;
            } else {
                return m;
            }
        }
        return l - 1;
    }

    public static void main(String[] args) {
        int x = 4;
        int x2 = 257;
        int x3 = 2147395600;
        Solution2 solution = new Solution2();
        System.out.println(solution.mySqrt(x));
        System.out.println(solution.mySqrt(x2));
        System.out.println(solution.mySqrt(x3));
    }
}
